<?php
require_once ('thirdparty/mail/mime.class.php');

class Mailer
{
    /**
     * Mailer
     *
     * @var Mail_Mime
     */
    var $sender;

    /**  Constructor
    */
    function Mailer($conf=array())
    {
        $this->sender = new Mail_Mime();
        $this->sender->setReturnPath($conf['return_path']);
        $this->sender->images_dir  = $conf['images_dir'];
        $this->sender->smtp_params = $conf['smtp'];
    }

   /**
     * Sends email
     *
     * @param string $mailName
     * @param string $to
     * @param array $vars
     * @param array $params
     * @param array $fileNames (name=>path)
     * @return boolean result of operation
     */
    function send($mailName, $to, $vars=array(), $params=array(), $fileNames=array())
    {
        if (!$to)
            return false;

        $db =Db::get(_cfg('db'));
        $sql = 'SELECT m_subject, m_text, m_html, m_fname, m_faddr'.
               ' FROM '._t('mail_letters').
               ' WHERE code="'.$db->escape($mailName).'"';
        $letter = $db->getRow($sql);
        if (! $letter)
            IError::fatal('mail.invalid_code: '.$mailName);

        if ($params)
            $letter = array_merge($letter, $params);

        $vars['MEMBER_LOGIN_LINK'] = _cfg('url.member');
        $vars['DATE']              = _dformat(time());
        if ($vars)
        {
            $letter['m_subject'] = $this->_compile($letter['m_subject'], $vars);
            $letter['m_text']    = $this->_compile(strip_tags($letter['m_text']), $vars);
            $letter['m_html']    = $this->_compile($letter['m_html'], $vars);
        }

        foreach ($fileNames as $name => $path)
            $this->sender->addAttachment(file_get_contents($path), $name);

        $this->sender->setSubject($letter['m_subject']);
        if ($letter['m_html'])
            $this->sender->setHtml($letter['m_html'], $letter['m_text']);
        else
            $this->sender->setText($letter['m_text']);

        $this->sender->setHeader('From', $letter['m_fname'].' <'.$letter['m_faddr'].'>');


		$retVal = true;
		
        if (! $this->sender->send(array($to),'mail'))
            $retVal = false;
        $f = fopen(_cfg("path")."/files/maillog.log","a+");
		ob_start();
		print_r("From: ".($letter['m_fname'].' <'.$letter['m_faddr'].'>')."\r\n");
		print_r("To: ".$to."\r\n");
		print_r("Subject: ".$letter['m_subject']);
		//print_r("Errors: \r\n");
		//print_r($this->sender);
		$msg = ob_get_contents();
		ob_clean();
		fwrite($f, $msg."\r\n=============================\r\n");
		fclose($f);
        
        return $retVal;
    }

    /**
     * Compliles string: replases vars
     *
     * @param string $str string to compile
     * @param array $vars vars/values
     * @return string compiled
     */
    function _compile($str, $vars)
    {
        foreach ($vars as $k => $v)
          $str = str_replace('{'.strtoupper($k).'}',$v, $str);
        return $str;
    }
}

?>
